package com.freesky.web.manager.controller.item;

import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.vo.VOItem;
import com.freesky.web.manager.remote.order.ItemRemote;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author Freesky
 * @date 2017/7/29
 */
@Controller
@Api(description = "商品列表信息")
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemRemote itemRemote;

    @GetMapping("/page")
    public String page() {
        return "item/list";
    }

    @GetMapping("/add")
    public String add() {
        return "item/add";
    }

    @GetMapping("/update")
    public String update() {
        return "item/update";
    }

    @ResponseBody
    @GetMapping(value = "/{itemId}")
    @ApiOperation(value = "通过ID获取商品")
    public JSONResponse getItemById(@PathVariable(value = "itemId") String itemId){

        return itemRemote.getItemById(itemId);
    }

    @ResponseBody
    @GetMapping(value = "/list")
    @ApiOperation(value = "分页搜索排序获取商品列表")
    public DataTablesResult getItemList(
            @RequestParam(value = "draw") int draw,
            @RequestParam(value = "start") int start,
            @RequestParam(value = "length") int length,
            @RequestParam(value = "cid") int cid,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir){


        return itemRemote.getItemList(draw, start, length, cid, search, orderCol, orderDir);
    }

    @ResponseBody
    @GetMapping(value = "/listSearch")
    @ApiOperation(value = "多条件分页搜索排序获取商品列表")
    public DataTablesResult getItemSearchList(
            int draw,
            int start,
            int length,
            int cid,
            String searchKey,
            String minDate,
            String maxDate,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir){

        return itemRemote.getItemSearchList(draw, start, length, cid, searchKey, minDate, maxDate, search, orderCol, orderDir);
    }

    @ResponseBody
    @GetMapping(value = "/count")
    @ApiOperation(value = "获得商品总数目")
    public DataTablesResult getAllItemCount(){
        return itemRemote.getAllItemCount();
    }

    @ResponseBody
    @PutMapping(value = "/stop/{id}")
    @ApiOperation(value = "下架商品")
    public JSONResponse stopItem(@PathVariable(value = "id") String id){
        return itemRemote.stopItem(id);
    }

    @ResponseBody
    @PutMapping(value = "/start/{id}")
    @ApiOperation(value = "发布商品")
    public JSONResponse startItem(@PathVariable(value = "id") String id){
        return itemRemote.startItem(id);
    }

    @ResponseBody
    @DeleteMapping(value = "/del/{ids}")
    @ApiOperation(value = "删除商品")
    public JSONResponse deleteItem(@PathVariable(value = "ids") String[] ids){
        return itemRemote.deleteItem(ids);
    }

    @ResponseBody
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加商品")
    public JSONResponse addItem(@ModelAttribute VOItem voItem){
        return itemRemote.addItem(voItem);
    }

    @ResponseBody
    @PostMapping(value = "/update/{id}")
    @ApiOperation(value = "编辑商品")
    public JSONResponse updateItem(@PathVariable(value = "id") String id,@ModelAttribute VOItem voItem){
        return itemRemote.updateItem(id, voItem);
    }

}
